package com.campus.counseling.controller;

import com.campus.counseling.model.common.Result;
import com.campus.counseling.entity.SysUser;
import com.campus.counseling.model.dto.LoginDTO;
import com.campus.counseling.model.dto.RegisterDTO;
import com.campus.counseling.model.vo.LoginVO;
import com.campus.counseling.model.vo.UserVO;
import com.campus.counseling.service.EmailService;
import com.campus.counseling.service.SysUserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "认证管理")
@RestController
@RequestMapping("/api/auth")
@RequiredArgsConstructor
public class AuthController {

    private final SysUserService userService;
    private final EmailService emailService;

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result<LoginVO> login(@RequestBody LoginDTO loginDTO) {
        return Result.success(userService.login(loginDTO));
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public Result<Void> register(@RequestBody RegisterDTO dto) {
        // 设置状态
        if ("counselor".equals(dto.getRole())) {
            dto.setStatus("PENDING"); // 咨询师需要审核
        } else {
            dto.setStatus("ACTIVE"); // 普通用户直接激活
        }
        userService.register(dto);
        return Result.success();
    }

    @ApiOperation("获取当前用户信息")
    @GetMapping("/info")
    public Result<UserVO> getUserInfo() {
        return Result.success(userService.getCurrentUserInfo());
    }

    @PostMapping("/logout")
    @ApiOperation("退出登录")
    public Result<Void> logout() {
        userService.logout();
        return Result.success();
    }

    @PostMapping("/forgot-password")
    @Operation(summary = "发送重置密码验证码")
    public Result<Void> sendResetCode(@RequestBody Map<String, String> params) {
        String email = params.get("email");
        // 验证邮箱是否存在
        if (!userService.existsByEmail(email)) {
            return Result.error("该邮箱未注册");
        }
        
        String code = emailService.generateVerificationCode();
        emailService.sendVerificationCode(email, code);
        return Result.success();
    }

    @PostMapping("/verify-code")
    @Operation(summary = "验证重置密码验证码")
    public Result<Void> verifyCode(@RequestBody Map<String, String> params) {
        String email = params.get("email");
        String code = params.get("code");
        
        if (emailService.verifyCode(email, code)) {
            return Result.success();
        }
        return Result.error("验证码错误或已过期");
    }

    @PostMapping("/reset-password")
    @Operation(summary = "重置密码")
    public Result<Void> resetPassword(@RequestBody Map<String, String> params) {
        String email = params.get("email");
        String code = params.get("code");
        String newPassword = params.get("newPassword");
        
        if (!emailService.verifyCode(email, code)) {
            return Result.error("验证码错误或已过期");
        }
        
        userService.resetPassword(email, newPassword);
        return Result.success();
    }

    @GetMapping("/email")
    @Operation(summary = "根据用户名获取邮箱")
    public Result<String> getEmailByUsername(@RequestParam String username) {
        SysUser user = userService.getByUsername(username);
        if (user == null) {
            return Result.error("用户不存在");
        }
        return Result.success(user.getEmail());
    }
} 